package gui.item;

import gui.clientes.*;
import bin.Armazem;
import bin.Item;
import bin.clientes.Cliente;
import bin.clientes.Telefone;
import bin.control.Control;
import bin.exc.ECadastroInvalido;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class PainelCadastroItem extends javax.swing.JPanel {

    //Atributos
    private Armazem armazem;
    private ArrayList<Telefone> listaTelefones;
    private Item itemEdicao;
    
    //Construtor
    public PainelCadastroItem(Armazem armazem) {
        initComponents();
        
        this.txtId.setText(Control.getInstance().getItemID() + "");
        this.armazem = armazem;
        this.listaTelefones = new ArrayList<Telefone>();
    }

    //Setar foco no campo "Nome" (Componente primário para foco)
    public void focoCompPadrao()
    {
        this.txtNomeItem.requestFocus();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tipoCliente = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtNomeItem = new javax.swing.JTextField();
        lbIconClientes = new javax.swing.JLabel();
        spPreco = new javax.swing.JSpinner();
        jLabel5 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        spQtd = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jLabel2.setText("Nome:");

        jLabel3.setText("Preço:");

        lbIconClientes.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lbIconClientes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/Images/novoItem.png"))); // NOI18N

        spPreco.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, 999999.0d, 0.5d));

        jLabel5.setText("ID:");

        txtId.setEditable(false);

        jLabel1.setText("Quantidade estoque:");

        spQtd.setModel(new javax.swing.SpinnerNumberModel(0, 0, 99999, 1));

        jLabel4.setText("R$");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(28, 28, 28)
                        .addComponent(txtNomeItem))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(44, 44, 44)
                        .addComponent(txtId))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbIconClientes)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(28, 28, 28)
                        .addComponent(spPreco, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(spQtd, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNomeItem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(spPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(spQtd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                .addComponent(lbIconClientes)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public JTextField getTxtNomeItem()
    {
        return this.txtNomeItem;
    }
    
    //Jogar informações do cliente cujo sofrerá edição nos campos do painel
    public void editarItem(Item c)
    {
        itemEdicao = c;
        this.txtId.setText(c.getId() + "");
        this.txtNomeItem.setText(c.getNome());
        this.spPreco.setValue(c.getPreco());
        this.spQtd.setValue(c.getQtdEstoque());
    }
    
    //Concluir Edição do cliente.
    public void concluirEdicao() throws ECadastroInvalido
    {
        this.itemEdicao.setNome(this.txtNomeItem.getText());
        this.itemEdicao.setPreco((Double)this.spPreco.getValue());
        this.itemEdicao.setQtdEstoque((Integer)this.spQtd.getValue());
    }
    
    //Concluir Cadastro do cliente. Retorna exceção se os campos não estiverem preenchidos
    public void concluirCadastro() throws ECadastroInvalido
    {
        //Definir novo ID
        int id = Integer.parseInt(this.txtId.getText());
        String descricao = this.txtNomeItem.getText();
        double preco = (Double)this.spPreco.getValue();
        int qtdInicial = (Integer)this.spQtd.getValue();

        Item item = new Item(id,descricao,qtdInicial,preco);
        this.armazem.getListaItens().add(item);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lbIconClientes;
    private javax.swing.JSpinner spPreco;
    private javax.swing.JSpinner spQtd;
    private javax.swing.ButtonGroup tipoCliente;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtNomeItem;
    // End of variables declaration//GEN-END:variables
}
